package com.jingzhi.huimiao.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jingzhi.huimiao.bean.WordInfo;

/**
 * 随机获取错误答案的工具类
 * 
 * @author 陈鑫
 * 
 */
public class CXRadom {

	private static Context mcontext;

	public CXRadom(Context context) {
		super();
		mcontext = context;
		// TODO Auto-generated constructor stub
	}

	private static Cursor cursor;

	public static List<String> getWrongAn(String translation) {

		// 取“.”前面的 字符
				String aa = translation.substring(0, translation.indexOf("."));
		
		// 打开数据库输出流
		SQLdm s = new SQLdm();
		SQLiteDatabase db = s.openDatabase(mcontext);

		// 查询数据库中testid=1的数据

		List<String> list = new ArrayList<String>();

		cursor = db.rawQuery("select word_translation from word where word_translation like '"
				+ aa + "%' and word_translation!='" + translation + "';",null);

		while(cursor.moveToNext()) {
			list.add(cursor.getString(cursor.getColumnIndex("word_translation")));
//			Toast.makeText(TestActivity.this,wordinfo.wname, 0).show();
		}
		List<String> list2 = new ArrayList<String>();
		if(list.size() >2){
			int j = new Random().nextInt(list.size() - 2);
			int js = Math.abs(j);
			list2.add(list.get(js));
			list2.add(list.get(js + 1));
			list2.add(list.get(js + 2));
			list2.add(translation);
		}else{
			list2.add("n.管；隧道；电视机");
			list2.add("n.带子v.脱衣；剥夺");
			list2.add("v.催促；强烈要求");
			list2.add(translation);
		}
		
		System.out.println(list2);

		

		// 查询每一个小task中的 单词集合 list
		String sql2 = "select * from word where pid=58;";
		
		if(cursor !=null){
			cursor.close();
		}
		if(db!=null){
			db.close();
		}
		
		return list2;
		
	}
	
	/**
	 * 为汉译英提供错误答案
	 * @param translation
	 * @return
	 */
	public static List<String> getWrongAn2(String word) {

		// 取第一个 字符
				String aa = word.substring(0, 1);
		
		// 打开数据库输出流
		SQLdm s = new SQLdm();
		SQLiteDatabase db = s.openDatabase(mcontext);

		// 查询数据库中testid=1的数据

		List<String> list = new ArrayList<String>();

		cursor = db.rawQuery("select word_name from word where word_name like '"
				+ aa + "%' and word_name!='" + word + "';",null);

		while(cursor.moveToNext()) {
			list.add(cursor.getString(cursor.getColumnIndex("word_name")));
//			Toast.makeText(TestActivity.this,wordinfo.wname, 0).show();
		}
		List<String> list2 = new ArrayList<String>();
		if(list.size() >2){
			int j = new Random().nextInt(list.size() - 2);
			int js = Math.abs(j);
			list2.add(list.get(js));
			list2.add(list.get(js + 1));
			list2.add(list.get(js + 2));
			list2.add(word);
		}else{
			list2.add("add");
			list2.add("append");
			list2.add("DJ");
			list2.add(word);
		}
		
		System.out.println(list2);

		

		// 查询每一个小task中的 单词集合 list
//		String sql2 = "select * from word where pid=58;";
		
		if(cursor !=null){
			cursor.close();
		}
		if(db!=null){
			db.close();
		}
		
		return list2;
		
	}
}
